const prefix:string = 'reelrush-daily-'

function set(key: string, val:any){
  try{
    window.sessionStorage.setItem(`${prefix}${key}`, JSON.stringify(val))
  }catch(err){
    console.error(err)
  }
}

function get(key:string) {
  const value:string|null = window.sessionStorage.getItem(`${prefix}${key}`);
  try {
    return JSON.parse(value as string);
  } catch (err) {
    console.error(err);
    return value;
  }
}

function remove(key:string) {
  try {
    window.sessionStorage.removeItem(`${prefix}${key}`);
  } catch (err) {
    console.error(err);
  }
}

function clear() {
  try {
    window.sessionStorage.clear();
  } catch (err) {
    console.error(err);
  }
}

interface Session {
  set:(key: string, val: any) => void,
  get: (key: string) => unknown,
  remove: (key: string) => void,
  clear: () => void
}

const session: Session = {
  set,
  get,
  remove,
  clear
}

export default session